﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using WmsService.BusinessLogic;
using WmsService.Util;

namespace WmsService.Reports
{
    public class ReportPayApplyBillAop :IReportAopHandle
    {
        private const string SqlInStoreBillId =
        "select b.BillID as InStoreBillID from dbo.InvoiceBillInStoreBillDtl d,dbo.InStoreBillDtl b where d.InStoreBillDtlID = b.BillDtlID and d.InvoiceNo in (select InvoiceNo from dbo.PayApplyBillDtl where PayApplyBillID ='{0}'  )";
        #region IReportAopHandle Members

        public void Handle(System.Data.DataSet ds, List<ReportDetail> reportDetails)
        {
            //var factory = LogicFactoryCreator.CreateAbstractFactory();
            
            foreach (var reportDtl in reportDetails)
            {
                if (reportDtl.DbTableName.Equals("PayApplyBill", StringComparison.OrdinalIgnoreCase))
                {
                    var table = ds.Tables[reportDtl.DbTableName];

                    table.Columns.Add("佰万", typeof(String));
                    table.Columns.Add("十万", typeof(String));
                    table.Columns.Add("万", typeof(String));
                    table.Columns.Add("仟", typeof(String));
                    table.Columns.Add("佰", typeof(String));
                    table.Columns.Add("十", typeof(String));
                    table.Columns.Add("元", typeof(String));
                    table.Columns.Add("角", typeof(String));
                    table.Columns.Add("分", typeof(String));
                    table.Columns.Add("大写", typeof(String));
                    table.Columns.Add("入库单编号", typeof(String));

                    foreach (DataRow row in ds.Tables[reportDtl.DbTableName].Rows)
                    {
                        var changer = new MoneyToChineseChanger(decimal.Parse(row["TotalAmount"].ToString()));
                        row["佰万"] = changer.佰万;
                        row["十万"] = changer.十万;
                        row["万"] = changer.万;
                        row["仟"] = changer.仟;
                        row["佰"] = changer.佰;
                        row["十"] = changer.十;
                        row["元"] = changer.元;
                        row["角"] = changer.角;
                        row["分"] = changer.分;
                        row["大写"] = changer.ToString();

                        string sql = String.Format(SqlInStoreBillId, row["PayApplyBillID"].ToString().Trim());
                        var table2 = DataAccess.DataQuerier.GetDataTable(sql);
                        string inStoreBillIDs = "";
                        foreach (DataRow dataRow in table2.Rows)
                        {
                            inStoreBillIDs += (dataRow["InStoreBillID"].ToString().Trim() + Environment.NewLine);
                        }
                        row["入库单编号"] = inStoreBillIDs;
                    }
                }
                else if (reportDtl.DbTableName.Equals("PayApplyBillDtl", StringComparison.OrdinalIgnoreCase))
                {
                    var table = ds.Tables[reportDtl.DbTableName];
                    
                    table.Columns.Add("佰万", typeof(String));
                    table.Columns.Add("十万", typeof(String));
                    table.Columns.Add("万", typeof(String));
                    table.Columns.Add("仟", typeof(String));
                    table.Columns.Add("佰", typeof(String));
                    table.Columns.Add("十", typeof(String));
                    table.Columns.Add("元", typeof(String));
                    table.Columns.Add("角", typeof(String));
                    table.Columns.Add("分", typeof(String));

                    foreach (DataRow dataRow in ds.Tables[reportDtl.DbTableName].Rows)
                    {
                        var changer = new MoneyToChineseChanger(decimal.Parse(dataRow["Amount"].ToString()));
                        dataRow["佰万"] = changer.佰万;
                        dataRow["十万"] = changer.十万;
                        dataRow["万"] = changer.万;
                        dataRow["仟"] = changer.仟;
                        dataRow["佰"] = changer.佰;
                        dataRow["十"] = changer.十;
                        dataRow["元"] = changer.元;
                        dataRow["角"] = changer.角;
                        dataRow["分"] = changer.分;
                    }
                }
            }
        }

        #endregion
    }
}